package com.jadcon.harveycedars.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.jadcon.harveycedars.model.dormitory.Dormitory;
import com.jadcon.harveycedars.model.dormitory.DormitoryService;
import com.jadcon.harveycedars.model.dormitory.DormitoryValidator;
import com.jadcon.harveycedars.model.dormitory.Room;

@Controller
@SessionAttributes("newDorm")
public class DormitoryController {

	@Autowired
	private DormitoryService dormitoryService;

	@RequestMapping("/dormitory.html")
	public ModelAndView dormitory() {
		System.out.println("DormitoryController.dormitory() begin...");
		
		// get data from model
		List<Dormitory> dormitoryList = dormitoryService.getDormList();
		int totalGuests = dormitoryService.getTotalGuests();
		int totalBeds = dormitoryService.getTotalBeds();
		int totalBedsAvailable = dormitoryService.getTotalBedsAvailable();
		
		// add data to ModelAndView
		ModelAndView mav = new ModelAndView("dormitory"); // "dormitory" is the view name
		mav.addObject("dormitoryList", dormitoryList);
		mav.addObject("totalGuests", totalGuests);
		mav.addObject("totalBeds", totalBeds);
		mav.addObject("totalBedsAvailable", totalBedsAvailable);
		return mav;
	}

	@RequestMapping("/dormitoryDetail.html")
	public ModelAndView dormitoryDetail(@RequestParam int id) {
		System.out.println("DormitoryController.dormitoryDetail() begin...");
		
		// get data from model
		Dormitory dormitory = dormitoryService.getDorm(id);
		if (dormitory == null) {
			// dormitory is not found for that id
			return new ModelAndView("dormitoryDetailError");
		} else {
			ModelAndView mav = new ModelAndView("dormitoryDetail"); // "dormitoryDetail" is the view name
			mav.addObject("dormitory", dormitory);
			return mav;
		}
	}
	
	// method that sets up the form
	@RequestMapping("/addDormitory.html")
	public ModelAndView addDormitory() {
		System.out.println("DormitoryController.addDormitory() begin...");

		Dormitory newDorm = new Dormitory();		
		ModelAndView mav = new ModelAndView("addDormitory");
		mav.addObject("newDorm", newDorm);
		return mav;
	}
	
	// method that handles the form submission
	@RequestMapping("/saveNewDormitory.html")
    public ModelAndView saveNewDormitory(@RequestParam String submit, @ModelAttribute("newDorm") Dormitory dormitory, BindingResult result) {
		System.out.println("DormitoryController.addDormitory() " + dormitory);
		
		
		if ("Add Room".equals(submit)) {
			Room room = new Room();
			dormitory.addRoom(room);
			
		} else {
			
			DormitoryValidator dormitoryValidator = new DormitoryValidator();
			dormitoryValidator.validate(dormitory, result);
	        if (!result.hasErrors()) {
	        	dormitoryService.save(dormitory);
				// rather than create its own view, you actually want to be
				// directed back to the dormitory list page, so call that method
				return dormitory();
	        }

		}
		// display the form again
		ModelAndView mav = new ModelAndView("addDormitory");
		mav.addObject("newDorm", dormitory);
		return mav;
	}	

}
